package com.springcloud.sc.ucpv5.client.commondata;

import java.util.ArrayList;

import com.springcloud.sc.ucpv5.client.commondata.data.Node;
import com.springcloud.sc.ucpv5.client.commondata.data.NodeStatus;
import com.springcloud.sc.ucpv5.client.commondata.tree.IDcsTreeNode;
import com.springcloud.sc.ucpv5.client.contentmain.MainConstant;
import com.springcloud.sc.ucpv5.client.module.datamanager.BaseDataManager;
import lombok.Data;

/**
 * 服务器结点
 * 
 * @author1
 *
 */
@Data
public class MyNode extends ModelBase {

	public static int INIT_TYPE_ONLINE = 1;
	public static int INIT_TYPE_NO_LOAD = 2;
	public static int INIT_TYPE_OFFLINE = 3;

	private volatile int isInit = INIT_TYPE_NO_LOAD;

	public boolean isNodeInit() {
		return isInit != INIT_TYPE_NO_LOAD;
	}

	private volatile String loadingTag = null;

	private Node idlData;
	private String ip;
	private NodeStatus status = new NodeStatus();

	public MyNode(Node idlData) {
		setIdlData(idlData);
		BaseDataManager.getInstance().nodeAdd(this);
	}

	public MyNode(String physics_NodeNumber, String logicNodeNumber) {
		this.ip = "";
		this.idlData = new Node();
		this.idlData.NodeIP = ip;
		this.idlData.NodeName = logicNodeNumber;
		this.idlData.Physics_NodeNumber = physics_NodeNumber;
		this.idlData.NodeNumber = logicNodeNumber;
		BaseDataManager.getInstance().nodeAdd(this);
	}

	public BaseListModel getListModel() {
		return baseListModel;
	}

	public boolean isConnected() {
		return status.Status == 0;
	}

	public String getPhysicsNodeNumber() {
		return idlData == null ? "" : idlData.Physics_NodeNumber;
	}

	public String getLogic_NodeNumber() {
		return idlData == null ? "" : idlData.NodeNumber;
	}

	public String getIp() {
		return ip == null ? idlData.NodeIP : ip;
	}

	public int getNodeType() {
		return idlData == null ? 0 : idlData.NodeType;
	}

	public boolean isVirtual() {
		return MainConstant.isVirtualNodeType(getNodeType());
	}

	public String getLabel() {
		return idlData != null ? idlData.NodeName : ip;
	}

	public void invokeAction(int actionId) {
	}

	/**
	 * notify 是在定义是否刷新界面
	 * 
	 * @param notify
	 * @return
	 */
	public boolean isload = false;

	public boolean isBlink() {
		return false;
	}

	public String getFieldText(int fieldId) {
		switch (fieldId) {
		case MainConstant.FIELD_TYPE_NAME:
			return getLabel();
		case MainConstant.FIELD_TYPE_ID:
			return getLogic_NodeNumber();
		case MainConstant.FIELD_TYPE_NODE_Number:
		case MainConstant.FIELD_TYPE_DEFAULT_SEC_LABEL:
			return getLogic_NodeNumber();
		case MainConstant.FIELD_TYPE_BackgroudImg:
			return idlData == null ? "" : idlData.Background;
		case MainConstant.FIELD_TYPE_ParentNumber:
			return idlData == null ? "" : idlData.ParentNumber;
		case MainConstant.FIELD_TYPE_IP:
			return idlData == null ? "" : idlData.NodeIP;
		case MainConstant.FIELD_TYPE_USER_NUM:
			return "";
		}
		return "--";
	}

	public int getIdField(int fieldId) {
		// TODO 自动生成方法存根
		return 0;
	}

	public ArrayList getChildren() {
		ArrayList ret = new ArrayList();
		for (MyNode node : baseListModel.getNodeList()) {
			if (node.isVirtual())
				continue;
			ret.add(node);
		}
		return ret;
	}

	public String getTreeLabel() {
		// 采用后台的查询组统计信息
		StringBuffer buffer = new StringBuffer();
		buffer.append(idlData.NodeName);
		return buffer.toString();
	}

	public boolean hasChildren() {
		return getChildren().size() > 0;
	}

	public void setParent(IDcsTreeNode parent) {
		super.setParent(parent);
	}

	public Node getIdlData() {
		return idlData;
	}

	public void setIdlData(Node idlData) {
		this.idlData = idlData;
		this.ip = idlData.NodeIP;
	}

	public int getSortLevel() {
		return getLevel() + 1000 + (idlData.NodeType * 100);
	}

	public String getSortLabel(int fieldId) {
		return null;
	}

	public String getSortKeyLabel() {
		return null;
	}

	public boolean isOnline() {
		return status.Status == MainConstant.NODE_Status_Online;
	}

	public boolean isSupportConnect(int nodeType) {
		return true;
	}

	public boolean isChildrenOf(String logicNodeId) {
		if (logicNodeId == null) {
			return false;
		}

		MyNode node = (MyNode) getParent();
		if (node != null) {
			if (isMe(logicNodeId)) {
				return true;
			}

			return node.isChildrenOf(logicNodeId);
		}

		return false;
	}

	public boolean isMe(String logicNodeId) {
		if (logicNodeId == null) {
			return false;
		}

		return logicNodeId.equals(getLogic_NodeNumber());
	}

	public String toString() {
		return getLabel();
	}

	public int getIsInit() {
		return isInit;
	}

	public void setIsInit(int isInit) {
		this.isInit = isInit;
	}

	public String getTipLabel() {
		StringBuffer buf = new StringBuffer();
		int[] supportField = getSupportField();
		String[] fieldNames = getFieldNames();
		for (int i = 0, n = Math.min(supportField.length, fieldNames.length); i < n; ++i) {
			buf.append(fieldNames[i] + ":");
			buf.append(getFieldText(supportField[i]));

			if (i < Math.min(supportField.length, fieldNames.length) - 1) {
				buf.append("\n");
			}
		}
		return buf.toString();
	}

	public boolean equals(Object tmp) {
		if (!(tmp instanceof MyNode)) {
			return false;
		}

		MyNode node = (MyNode) tmp;
		if (node.getLogic_NodeNumber().equals(this.getLogic_NodeNumber())) {
			return true;
		}

		return false;
	}

	public int hashCode() {
		return getLogic_NodeNumber().hashCode() << 8;
	}


	public int[] getSupportField() {
		return null;
	}


	public String[] getFieldNames() {
		return null;
	}

}
